package com.zp.oj.model.vo;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.zp.oj.judge.model.JudgeInfo;
import com.zp.oj.model.entity.QuestionSubmit;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.Date;

@Data
public class QuestionSubmitVO implements Serializable {
    /**
     * id
     */
    private Long id;

    /**
     * 帖子 id
     */
    private Long questionId;

    /**
     * 代码语言
     */
    private String language;

    /**
     * 创建用户 id
     */
    private Long userId;

    /**
     * 用户提交代码
     */
    private String code;


    /**
     * 判题状态 0待判题 1判题中 2成功 3失败
     */
    private Integer status;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 提交用户信息
     */
    private UserVO userVO;

    /**
     * 对应问题信息
     */
    private String questionTitle;

    /**
     * 判题信息
     */
    private JudgeInfo judgeInfo;

    /**
     * 额外判题信息
     */
    private String extraMessage;


    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    /**
     * 包装类转对象
     *将 VO对象转 entity对象
     * @param questionSubmitVO
     * @return
     */
    public static QuestionSubmit voToObj(QuestionSubmitVO questionSubmitVO) {
        if (questionSubmitVO == null) {
            return null;
        }
        QuestionSubmit questionSubmit = new QuestionSubmit();
        String judgeInfo = JSONUtil.toJsonStr(questionSubmitVO.judgeInfo);
        BeanUtils.copyProperties(questionSubmitVO, questionSubmit);
        questionSubmit.setJudgeInfo(judgeInfo);
        return questionSubmit;
    }

    /**
     * 对象转包装类
     *将 entity对象转 VO对象
     * @param questionSubmit
     * @return
     */
    public static QuestionSubmitVO objToVo(QuestionSubmit questionSubmit) {
        if (questionSubmit == null) {
            return null;
        }
        QuestionSubmitVO questionSubmitVO = new QuestionSubmitVO();
        BeanUtils.copyProperties(questionSubmit, questionSubmitVO);
        JudgeInfo judgeInfo = JSONUtil.toBean(questionSubmit.getJudgeInfo(), JudgeInfo.class);
        questionSubmitVO.setJudgeInfo(judgeInfo);
        return questionSubmitVO;
    }
}